home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3794 / 3794.xpi / chrome / content / login.js < prev    next >
Text File  |  2009-08-13  |  3KB  |  90 lines

  1. /**
  2.  *
  3.  * The source code included in this file is licensed to you by Facebook under
  4.  * the Apache License, Version 2.0.  Accordingly, the following notice
  5.  * applies to the source code included in this file:
  6.  *
  7.  * Copyright ┬⌐ 2009 Facebook, Inc.
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10.  * not use this file except in compliance with the License. You may obtain
  11.  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16.  * License for the specific language governing permissions and limitations
  17.  * under the License.
  18.  *
  19.  */
  20.  
  21.  
  22. function debug(s) { dump('** login.js: [' + s + ']\n'); }
  23.  
  24. var Cc = Components.classes;
  25. var Ci = Components.interfaces;
  26.  
  27. // load FacebookLoginClient code
  28. Cc['@mozilla.org/moz/jssubscript-loader;1']
  29.     .getService(Ci.mozIJSSubScriptLoader)
  30.     .loadSubScript('chrome://facebook/content/facebook.js');
  31.  
  32. var client = new FacebookLoginClient();
  33. var fbns   = new Namespace( "http://api.facebook.com/1.0/" );
  34. function startup() {
  35.     if (client.fbSvc.loggedIn) {
  36.         debug('already logged in!');
  37.         window.close();
  38.     } else if (!client.authToken) {
  39.         debug('requesting token');
  40.         try {
  41.         client.callMethod('facebook.auth.createToken', [], function(req) {
  42.             debug('received token response:');
  43.             debug( req.responseText); 
  44.             client.authToken = req.xmldata;
  45.             debug('token is: '+client.authToken);
  46.             startup();
  47.         });
  48.         } catch (e) {
  49.             debug('exception: ' + e);
  50.         }
  51.     } else {
  52.         var browser = document.getElementById('facebook-login-body');
  53.         var login_base = 'http://www.facebook.com/login.php?popup&v=1.0&api_key=';
  54.         browser.setAttribute('src', login_base +
  55.                              client.fbSvc.apiKey + '&auth_token=' + client.authToken);
  56.         browser.style.display = '';
  57.         document.getElementById('throbber-box').style.display = 'none';
  58.         debug('loading login page');
  59.     }
  60. }
  61. window.addEventListener('load', startup, false);
  62.  
  63. function done() {
  64.     debug('done()');
  65.     if (!client.authToken) {
  66.         window.close();
  67.         return false;
  68.     }
  69.     debug(client.authToken);
  70.     client.callMethod('facebook.auth.getSession', ['auth_token='+client.authToken], function(req) {
  71.         debug('received session response:');
  72.         debug(req.xmldata);
  73.         var data = req.xmldata;
  74.         var sessionKey    = data.fbns::session_key;
  75.         var sessionSecret = data.fbns::secret;
  76.         var uid           = data.fbns::uid;
  77.         if (sessionKey && sessionSecret && uid) {
  78.             client.fbSvc.sessionStart(sessionKey, sessionSecret, uid);
  79.             client.authToken  = null;
  80.             debug('session: ' + sessionKey + ', uid: ' + uid + ', secret: ' + sessionSecret);
  81.         }
  82.         window.setTimeout('window.close()', 1);
  83.     });
  84.     // in case the request fails, let's just force a 4 second timeout
  85.     window.setTimeout('window.close()', 4000);
  86.     return false;
  87. }
  88.  
  89. dump('loaded login.js');
  90.